<?php
include_once("functions/db_manipulate.php");
$link_id = connectDB();
$newsResultSet = mysql_query("SELECT
                                     header,
                                     news.id,
                                     content,
                                     date,
                                     time,
                                     nick AS author,
                                     path
                              FROM news
                              INNER JOIN users ON  news.author = users.id
                              INNER JOIN newsimages ON news.id = newsimages.newsid
                              WHERE
                                        newsimages.main = '1'
                                    AND
                                        news.id='" . mysql_real_escape_string($_GET['nid']) . "'");

$newsRow = mysql_fetch_assoc($newsResultSet);

if ($_GET['page'] == '') {
    $start = 1;
    $page = 1;
} else {
    $start = ($_GET['page'] - 1) * 20 + 1;
    $page = $_GET['page'];
}
$responsesResult = getResponseComments($start, $start + 19, $_GET['nid']);

$pages = countPages($_GET['nid']);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>Последние новости - theFlyNet.com</title>
    <link rel="stylesheet" type="text/css" href="css/navigation.css">
    <link rel="stylesheet" type="text/css" href="css/fullnews.css">
    <link rel="stylesheet" type="text/css" href="css/comment.css">
    <link rel="stylesheet" type="text/css" href="css/square.css">
    <link rel="stylesheet" type="text/css" href="css/menu.css">
    <link rel="stylesheet" type="text/css" href="css/common.css">
    <script src="resources/jquery.js"></script>

    <meta charset="UTF-8">
    <script language="JavaScript">
        function reply(i) {
            $('#reply').val(i);
            $('#reply2').val(i);
            $('#textarea').focus();
            $('#textarea').attr("placeholder", "Вы процитировали сообщение, введите ваш ответ.. ")
        }

        function customFormSubmit() {
            $('#maximized_navigation').show();
            $('#commentform1').submit();
        }

    </script>
</head>
<body class="body">

<?php
include_once("menu.php");
?>

<div id="square">
<div class="fullnews">
    <div id="header">
        <?= htmlspecialchars($newsRow['header']) ?>
    </div>
    <img src="news_img/<?= $newsRow['path'] ?>" id="imagesdiv">

    <div id="content">
        <?= htmlspecialchars($newsRow['content']) ?>
    </div>
    <?php
    $newsImagesResultSet = getNewsImages($newsRow['id']);
    while ($newsImage = mysql_fetch_assoc($newsImagesResultSet)) {
        ?>
        <img src="news_img/<?= $newsImage['path'] ?>" id="imagesdiv">
    <?php
    }
    ?>
    <div id="newsinfo">
        <div id="author">
            <?= $newsRow['author'] ?>
        </div>
        <div id="date">
            <?= $newsRow['date'] ?>
        </div>
        <div id="date">
            <?= $newsRow['time'] ?>
        </div>
    </div>
</div>

<div class="commentsheader">
    Комментарии пользователей:
</div>
<?php
if ($commentRow = mysql_fetch_assoc($responsesResult)) {
    ?>

    <div id="pages">
        <?php

        $pagesStart = 0;
        $pagesEnd = 0;
        if ($pages <= 5) {
            $pagesStart = 1;
            $pagesEnd = $pages;
        } else if ($pages >= 5 && $page + 2 <= $pages) {
            $pagesStart = $page - 2;
            $pagesEnd = $page + 2;
        } else if ($pages >= 5 && $page + 2 > $pages) {
            $pagesStart = $pages - 4;
        }

        if ($pages <= 1) {
        } else {
            if ($page != 1) {
                ?>
                <div class="page" onclick="location.href='news.php?nid=<?= $_GET['nid'] ?>&page=1'">
                    <<
                </div>
            <?php
            }
            for ($i = $pagesStart; $i <= $pagesEnd; $i++) {
                ?>
                <div class="page" onclick="location.href='news.php?nid=<?= $_GET['nid'] ?>&page=<?= $i ?>'">
                    <?= $i ?>
                </div>
            <?php
            }
            if ($page != $pages) {
                ?>
                <div class="page" onclick="location.href='news.php?nid=<?= $_GET['nid'] ?>&page=<?= $pages ?>'">
                    >>
                </div>
            <?php
            }
        }
        ?>
    </div>

    <?php
    $comments = 0;

    do {
        $comments++;
        ?>
        <div class="comment">

            <div class="commentinfodiv">
                <div class="commentauthor">
                    #<?= $commentRow['id'] ?>
                </div>
                <div class="commentdate">
                    <?= $commentRow['date'] ?>
                </div>
                <div class="commentdate">
                    <?= $commentRow['time'] ?>
                </div>
            </div>

            <div>

                <div class="avatardiv">
                    <div>
                        <img src="user_img/<?= $commentRow['image'] ?>" class="avatarimg">
                    </div>
                    <div class="userinfo">
                        <div>
                            Ник: <?= getTextWithAcronym($commentRow['author'], 15) ?>
                        </div>
                        <div>
                            Регистрация: <?= $commentRow['registered'] ?>
                        </div>
                        <div>
                            Сообщений: <?= $commentRow['messages'] ?>
                        </div>
                        <div>
                            Почта: <?= getTextWithAcronym($commentRow['email'], 22) ?>
                        </div>
                    </div>
                </div>

                <div class="commentdiv">
                    <?php if ($commentRow['quote'] != 0) { ?>
                        <div class="commentquote">
                            <?php
                            $quoteRow = mysql_fetch_assoc(getResponseComment($commentRow['quote']));
                            ?>
                            <?= htmlspecialchars($quoteRow['content']); ?>
                        </div>
                    <?php
                    }

                    $imgResultSet = getResponseImages($commentRow['id']);

                    while ($imgRow = mysql_fetch_assoc($imgResultSet)) {
                        ?>
                        <div>
                            <img class="commentimg" src="upload/<?= $imgRow['path'] ?>"/>
                        </div>
                    <?php
                    }
                    ?>
                    <?= htmlspecialchars($commentRow['content']) ?>
                </div>
            </div>

            <div class="manipulationdiv" style="<?php if (!init()) echo "display: none;" ?>">
                <div class="manipulationbutton" <?php if (!isLiked("r", $commentRow['id'])) {
                    ?>
                    onclick="location.href = 'like_handler.php?rid=<?= $commentRow['id'] ?>&nid=<?= $_GET['nid'] ?>&like=1'"
                <?php
                }
                ?>
                    >
                    +(<?= $commentRow['likes'] ?>)
                </div>
                <div class="manipulationbutton"
                    <?php if (!isDisiked("r", $commentRow['id'])) {
                        ?>
                        onclick="location.href = 'like_handler.php?rid=<?= $commentRow['id'] ?>&nid=<?= $_GET['nid'] ?>&dislike=1'"
                    <?php
                    }
                    ?>>
                    -(<?= $commentRow['dislikes'] ?>)
                </div>
                <?php if (!isSpammed("r", $commentRow['id'])) { ?>

                    <div class="manipulationbutton"
                         onclick="location.href = 'spam_handler.php?rid=<?= $commentRow['id'] ?>'">
                        Спам
                    </div>
                <?php
                }
                ?>
                <a href="#anchor">
                    <div class="manipulationbutton"
                         style="margin-left: 160px;"
                         onclick="reply('<?= $commentRow['id'] ?>')">Ответить
                    </div>
                </a>
            </div>

        </div>

    <?php
    } while ($commentRow = mysql_fetch_assoc($responsesResult));
    ?>

    <div id="pages">
        <?php

        $pagesStart = 0;
        $pagesEnd = 0;

        if ($pages <= 5) {
            $pagesStart = 1;
            $pagesEnd = $pages;
        } else if ($pages >= 5 && $page + 2 <= $pages) {
            $pagesStart = $page - 2;
            $pagesEnd = $page + 2;
        } else if ($pages >= 5 && $page + 2 > $pages) {
            $pagesStart = $pages - 4;
            $pages;
        }
        if ($pages <= 1) {
        } else {
            if ($page != 1) {
                ?>
                <div class="page" onclick="location.href='news.php?nid=<?= $_GET['nid'] ?>&page=1'">
                    <<
                </div>
            <?php
            }
            for ($i = $pagesStart; $i <= $pagesEnd; $i++) {
                ?>
                <div class="page" onclick="location.href='news.php?nid=<?= $_GET['nid'] ?>&page=<?= $i ?>'">
                    <?= $i ?>
                </div>
            <?php
            }
            if ($page != $pages) {
                ?>
                <div class="page" onclick="location.href='news.php?nid=<?= $_GET['nid'] ?>&page=<?= $pages ?>'">
                    >>
                </div>
            <?php
            }
        }
        ?>
    </div>



    <a id="anchor">

        <div class="common" <?php if ($comments > 5) display() ?> >
            <form enctype="multipart/form-data" accept-charset="UTF-8" action="comment_handler.php" method="post"
                  id="commentform1">
                <span style="font-weight: bold">Новый комментарий:</span>
                <textarea name="content" id="textarea" rows="11" placeholder="Введите ваш комментарий"></textarea>
                <input type="hidden" name="MAX_FILE_SIZE" value="25000000"/>

                <div style="display: inline-block; width: 100%">
                    <div id="fileinputdiv">
                        <input name="images[]" type="file" accept=".jpg, .png, .jpeg, .gif" id="realfileinput" multiple>

                        <div id="fileinput">Изображения</div>
                    </div>

                    <div id="commentbutton" onclick="customFormSubmit()">Продолжить</div>

                </div>

                <input type="hidden" name="nid" value="<?= $_GET['nid'] ?>">
                <input type="hidden" name="reply" id="reply">
            </form>
        </div>
    </a>



<?php
} else if (init()) {
    ?>
    <div id="nocomments">
        К данной новости еще нет комментариев. Ваш может быть первым :)
    </div>

    <a id="anchor">

        <div class="common" <?php if ($comments > 5) display() ?> >
            <form enctype="multipart/form-data" accept-charset="UTF-8" action="comment_handler.php" method="post"
                  id="commentform1">
                <span style="font-weight: bold">Новый комментарий:</span>
                <textarea name="content" id="textarea" rows="11" placeholder="Type your comment here.."></textarea>
                <input type="hidden" name="MAX_FILE_SIZE" value="25000000"/>

                <div style="display: inline-block; width: 100%">
                    <div id="fileinputdiv">
                        <input name="images[]" type="file" accept=".jpg, .png, .jpeg, .gif" id="realfileinput" multiple>

                        <div id="fileinput">Изображения</div>
                    </div>

                    <div id="commentbutton" onclick="customFormSubmit()">Продолжить</div>

                </div>
                <input type="hidden" name="nid" value="<?= $_GET['nid'] ?>">
                <input type="hidden" name="reply" id="reply">
            </form>
        </div>
    </a>


<?php
} else {
    ?>
    <div id="nocomments">
        К данной новости еще нет комментариев. Ваш может быть первым :)
    </div>
<?php
}
?>
</div>


<?php
include_once("navigation.html");
?>

</body>
</html>